<?php
/**
 * Created by PhpStorm.
 * User: mr_lee
 * Date: 2017/10/22
 * Time: 上午9:49
 */

namespace app\leader\controller;

use app\common\controller\AdminBase;

use app\common\model\MerchantCate;
use think\Db;

class Sysconfig extends AdminBase
{

    /*
     * 分类管理
     */
    public function category()
    {
        $MerchantCate = new MerchantCate();
        $level = $MerchantCate->where(['status' => 0])->select();
        $this->assign('level', $level);
        $list = collection($level)->toArray();
        foreach ($list as $k => $v) {
            $list2[$k]['id'] = $v['id'];
            $list2[$k]['level'] = $v['level'];
            $list2[$k]['parent_id'] = $v['parent_id'];
            $list2[$k]['del'] = "删除";
            $list2[$k]['modify'] = "修改;";
        }
        $where = [];
        $map['status'] = 0;
        $tree = new Tree();
        $tree->init($list2);
        $parentid = isset($where['parentid']) ? $where['parentid'] : 0;
        $tree->nbsp = '&nbsp;&nbsp;&nbsp;';
        $str = "<tr>
                    <td>\$id</td>
                    <td>\$spacer  \$level</td>
                    <td> <a href='javascript:;' onclick='xg(\$id)'>修改&nbsp;&nbsp;</a>|<a href='javascript:;' data-id='\$id' class='delete'>\$del </a></td>
                    <td></td>
                </tr>";
        $info = $tree->get_tree(0, $str);
        $this->assign('info', $info);
        return $this->fetch();
    }

    /*
     * 分类修改
     */
    public function category_modify()
    {
        if (request()->isAjax()) {
            if (request()->isGet()) {
                $map['id'] = request()->get('id');
                $MerchantCate = new MerchantCate();
                $category = $MerchantCate->where($map)->find();
                $this->assign('category', $category);
                $level = $MerchantCate->where(array('parent_id' => 0))->select();
                $this->assign('level', $level);
                return $this->fetch();
            }
            if (request()->isPost()) {
                $MerchantCate = new MerchantCate();
                $data = request()->post();
                // 验证通过 可以进行其他数据操作
                $add = $MerchantCate->save($data, ['id' => $data['id']]);
                if ($add) {
                    $data = array('code' => 0, 'message' => '修改成功');
                } else {
                    $data = array('code' => 9999, 'message' => '修改失败');;
                }
                return json($data);
            }
        }
    }

    /*
     * 分类添加
     * */
    public function category_add()
    {
        if (request()->isAjax()) {
            $MerchantCate = new MerchantCate();
            $add = $MerchantCate->save(request()->post());
            if ($add) {
                $data = array('code' => 0, 'message' => '修改成功');
            } else {
                $data = array('code' => 9999, 'message' => '修改失败');;
            }
        }
      return json($data);
    }

    /*
     * 分类删除
     * */
    public function category_rec(){
        if (request()->isAjax()) {
            $MerchantCate = new MerchantCate();
            $add = $MerchantCate->where(['id'=>request()->post('id')])->delete();
            if ($add) {
                $data = array('code' => 0, 'message' => '删除成功');
            } else {
                $data = array('code' => 9999, 'message' => '删除失败');;
            }
            return json($data);
        }
    }

}